package com.wushi.lenovo.asia5b.app_ui.good.adapter;

import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.images.WImageLoader;
import com.mvp.utils.StringUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.OrderGoodBean;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemModel;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemType;
import com.wushi.lenovo.asia5b.app_ui.good.listener.ButtonListener;
import com.wushi.lenovo.asia5b.app_ui.good.listener.OrderAdapterListener;

import java.util.List;

/**
 * Created by zhangyuncai on 2020/3/3.
 * button_pay_price_diff
 * button_pay_order
 */
public class OrderAdapter extends BaseQuickAdapter<OrderItemModel, BaseViewHolder> {
    final OrderItemType type;

    public static OrderAdapter newInstance(OrderItemType type, @Nullable List<OrderItemModel> data, OrderAdapterListener orderAdapterListener) {
        if (type == OrderItemType.Verification) {
            return new OrderAdapter(type, R.layout.item_order_verifi_adapter, data, orderAdapterListener);
        } else if (type == OrderItemType.Payment) {
            return new OrderAdapter(type, R.layout.item_order_payment_adapter, data, orderAdapterListener);
        } else if (type == OrderItemType.Delivery) {
            return new OrderAdapter(type, R.layout.item_order_delivery_adapter, data, orderAdapterListener);
        } else if (type == OrderItemType.History) {
            return new OrderAdapter(type, R.layout.item_order_history_adapter, data, orderAdapterListener);
        } else if (type == OrderItemType.Problematic) {
            return new OrderAdapter(type, R.layout.item_order_problem_adapter, data, orderAdapterListener);
        }
        return null;
    }

    ButtonListener buttoListener;
    final OrderAdapterListener orderAdapterListener;

    public void setButtoListener(ButtonListener buttoListener) {
        this.buttoListener = buttoListener;
    }


    public OrderAdapter(OrderItemType type, int layoutResId, @Nullable List<OrderItemModel> data, OrderAdapterListener orderAdapterListener) {
        super(layoutResId, data);
        this.type = type;
        this.orderAdapterListener = orderAdapterListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderItemModel item) {
        if (type == OrderItemType.Verification) {
            setViewVerification(helper, item);
        } else if (type == OrderItemType.Payment) {
            setViewPayment(helper, item);
        } else if (type == OrderItemType.Delivery) {
            setViewDelivery(helper, item);
        } else if (type == OrderItemType.History) {
            setViewHistory(helper, item);
        } else if (type == OrderItemType.Problematic) {
            setViewProblem(helper, item);
        }
        /*
        如果是自营产品，不会在处理中和待付款订单出现的，直接会在发货中的，但是状态还没出仓，所以没有按键
         */
    }

    /**
     * 处理中
     *
     * @param helper
     * @param item
     */
    public void setViewVerification(BaseViewHolder helper, OrderItemModel item) {
        //订单信息
        setOrderInfo(helper, item);

        //查看订单详细
        TextView tv_order_details = helper.getView(R.id.tv_order_details);
        tv_order_details.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线

        //商品列表
        RecyclerView recyclerView = helper.getView(R.id.rv_order);
        if (item.getOrder_parcel() != null) {
            List<OrderGoodBean> goodsList = item.getOrder_parcel();
            OrderGoodsAdapter adapter = OrderGoodsAdapter.newInstance(type, goodsList, orderAdapterListener);
            adapter.setButtoListener(buttoListener);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(adapter);
        }
    }

    /**
     * 付款
     *
     * @param helper
     * @param item
     */
    public void setViewPayment(BaseViewHolder helper, OrderItemModel item) {
        //订单信息
        setOrderInfo(helper, item);

        //查看订单详细
        TextView tv_order_details = helper.getView(R.id.tv_order_details);
        tv_order_details.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线

        //商品列表
        RecyclerView recyclerView = helper.getView(R.id.rv_order);
        if (item.getOrder_parcel() != null) {
            List<OrderGoodBean> goodsList = item.getOrder_parcel();
            OrderGoodsAdapter adapter = OrderGoodsAdapter.newInstance(type, goodsList, orderAdapterListener);
            adapter.setButtoListener(buttoListener);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(adapter);
        }

        //商品小计
        TextView tv_order_subtotal = helper.getView(R.id.tv_order_subtotal);
        tv_order_subtotal.setText(String.format("%s %s", "RM", item.getOrder_sub_total()));
        //小计价==0时,隐藏小计价
        helper.setGone(R.id.ll_order_subtotal_vessel, StringUtils.parseFloat(item.getOrder_sub_total()) != 0);

        //订单按钮
        setButtonListView(helper, item);
        boolean hasAction = (item.getOrder_action() != null) && (item.getOrder_action().size() != 0);
        helper.setGone(R.id.ll_order_secondpay_vessel, hasAction);

    }

    /**
     * 运输中
     *
     * @param helper
     * @param item
     */
    public void setViewDelivery(BaseViewHolder helper, OrderItemModel item) {
        //商品图片
        ImageView iv_order_img = helper.getView(R.id.iv_order_img);
        WImageLoader.displayImage(mContext, item.getOrder_image(), iv_order_img);

        //商品数量
        TextView tv_order_goodsnum = helper.getView(R.id.tv_order_goodsnum);
        tv_order_goodsnum.setText(item.getOrder_count_text());

        setOrderInfo(helper, item);

        //订单按钮
        setButtonListView(helper, item);
    }

    /**
     * 历史
     *
     * @param helper
     * @param item
     */
    public void setViewHistory(BaseViewHolder helper, OrderItemModel item) {
        //商品图片
        ImageView iv_order_img = helper.getView(R.id.iv_order_img);
        WImageLoader.displayImage(mContext, item.getOrder_image(), iv_order_img);

        //商品数量
        TextView tv_order_goodsnum = helper.getView(R.id.tv_order_goodsnum);
        tv_order_goodsnum.setText(item.getOrder_count_text());

        setOrderInfo(helper, item);

        setOrderStateText(helper, item);

    }

    /**
     * 处理中
     *
     * @param helper
     * @param item
     */
    public void setViewProblem(BaseViewHolder helper, OrderItemModel item) {
        //订单信息
        setOrderInfo(helper, item);

        //查看订单详细
        TextView tv_order_details = helper.getView(R.id.tv_order_details);
        tv_order_details.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线

        //商品列表
        RecyclerView recyclerView = helper.getView(R.id.rv_order);
        if (item.getOrder_parcel() != null) {
            List<OrderGoodBean> goodsList = item.getOrder_parcel();
            OrderGoodsAdapter adapter = OrderGoodsAdapter.newInstance(type, goodsList, orderAdapterListener);
            adapter.setButtoListener(buttoListener);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(adapter);
        }
    }


    /**
     * 设置订单信息
     *
     * @param helper
     * @param item
     */
    public void setOrderInfo(BaseViewHolder helper, OrderItemModel item) {
        //订单号
        TextView tv_order_orderno = helper.getView(R.id.tv_order_orderno);
        tv_order_orderno.setText(TextUtils.isEmpty(item.getOrder_no()) ? "-" : item.getOrder_no());

        //时间
        TextView tv_order_time = helper.getView(R.id.tv_order_time);
        tv_order_time.setText(item.getOrder_date());

        //查看订单详情
        helper.getView(R.id.tv_order_details).setOnClickListener(v -> {
            orderAdapterListener.orderDetail(item);
        });
    }

    /**
     * 设置底部按键列表
     *
     * @param helper
     * @param orderItemModel 订单数据
     */
    private void setButtonListView(BaseViewHolder helper, OrderItemModel orderItemModel) {
        int position = helper.getLayoutPosition();
        //button列表
        RecyclerView recyclerView = helper.getView(R.id.recyclerview_order_button);
        //todo
        List<OrderGoodBean.ActionBean> buttonList = orderItemModel.getOrder_action();
        if (buttonList == null || buttonList.size() == 0) {
            recyclerView.setVisibility(View.GONE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            ButtonAdapter adapter = new ButtonAdapter(buttonList);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position2) {
                    OrderGoodBean.ActionBean actionBean = (OrderGoodBean.ActionBean) adapter.getItem(position2);
                    if (buttoListener != null && actionBean.getEnabled() == 1) {
                        buttoListener.actionClick(actionBean, orderItemModel, null);
                    }

                }
            });
        }
    }


    public void setOrderStateText(BaseViewHolder helper, OrderItemModel orderItemModel) {
        //商品订单状态
        TextView tv_order_type = helper.getView(R.id.tv_order_type);
        tv_order_type.setText(String.format("%s", orderItemModel.getOrder_status_name()));
        if (type == OrderItemType.Problematic) {
            //设置问题按钮左右icon样式
//            Drawable left = ContextCompat.getDrawable(mContext, R.drawable.ic_orderitem_info_white);
//            left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
//            Drawable right = ContextCompat.getDrawable(mContext, R.drawable.ic_orderitem_arrow_white);
//            right.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
//            tv_order_type.setCompoundDrawables(left,
//                    null,
//                    right,
//                    null);
            //添加监听
            helper.addOnClickListener(R.id.tv_order_type);
        }

        //设置包裹状态背景颜色
        tv_order_type.setBackgroundColor(orderItemModel.getOrder_status_color(mContext));
    }

}
